package com.x.reader.controller;

import com.x.framework.Base;
import com.x.framework.model.ResData;
import com.x.reader.model.History;
import com.x.reader.model.Video;
import com.x.reader.model.VideoChapter;
import com.x.reader.service.HistoryService;
import com.x.reader.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Controller
@RestController
public class VideoController extends Base {

    @Autowired
    private VideoService videoService;

    @Autowired
    private HistoryService historyService;

    @RequestMapping(value = "/SelectVideoList")
    public ResData selectVideoList(Video video) {
        List<Video> videoList = videoService.selectVideoList(video);
        ResData resData = new ResData(1);
        resData.setResult(videoList);
        return resData;
    }

    @RequestMapping(value = "/SelectVideoChapterList")
    public ResData selectVideoChapterList(VideoChapter videoChapter) {
        List<VideoChapter> videoChapterList = videoService.selectVideoChapterList(videoChapter);
        ResData resData = new ResData(1);
        resData.setResult(videoChapterList);
        return resData;
    }

    @RequestMapping(value = "/SelectVideoChapter")
    public ResData selectVideoChapter(VideoChapter videoChapter, String userId) {
        List<VideoChapter> videoChapterList = videoService.selectVideoChapterList(videoChapter);
        videoChapter = videoChapterList.get(0);

        if (Base.isString(userId)) {
            History history = new History();
            history.setUserId(userId);
            history.setHistoryId(videoChapter.getVideoChapterId());
            history.setHistoryTitle(videoChapter.getVideoChapterTitle());
            history.setHistoryCover(videoChapter.getVideoChapterCover());
            history.setHistoryCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            history.setCategoryId(2);
            historyService.insertHistory(history);
        }

        ResData resData = new ResData(1);
        resData.setResult(videoChapter);
        return resData;
    }

}
